const query = require("../db/query");

// 医院列表
const nosocomiumList = async (params) => {
  const data = {
    pageCode: params.pageCode ? params.pageCode : 1,
    pageSize: params.pageSize ? params.pageSize : 10,
    lv: params.lv ? params.lv : "",
    city: params.city ? params.city : "",
    state: params.state ? params.state : "",
    search: params.search ? params.search : "",
    admin: params.admin ? params.admin : "",
  };
  let sql = await query(
    `select * from soco where socolv like '%${data.lv}%' and sococity like '%${
      data.city
    }%' and socostate like '%${data.state}%' and  nosocomium like '%${
      data.search
    }%' and socoadmin like '%${data.admin}%' limit ${
      (data.pageCode - 1) * data.pageSize
    }, ${data.pageSize}`
  );
  let sql1 = await query(`select * from soco`);
  return { data: sql, total: sql1.length };
};

// 医院详情
const nosocomiumDetail = (params) => {
  let sql = `SELECT * from soco a join doctorlist b on a.socoid = b.doctoradmin where a.socoid = ${params.id}`;
  return query(sql);
};

// 医院删除
const nosocoDelete = (params) => {
  let sql = `delete from soco where socoid = ${params.id}`;
  return query(sql);
};

// 医院新增
const nosocoAdd = (params) => {
  let sql = `insert into soco (nosocomium,socolv,sococity,socostate,socoadmin,socodescword,socoimage) values ('${params.name}','${params.lv}','${params.city}','${params.state}','${params.admin}','${params.desc}','${params.image}')`;
  return query(sql);
};

// 医院修改
const nosocoUpdate = (params) => {
  let sql = `update soco set nosocomium = '${params.name}',socolv = '${params.lv}',sococity = '${params.city}',socostate = '${params.state}',socoadmin = '${params.admin}',socodescword = '${params.desc}',socoimage = '${params.image}' where socoid = '${params.id}'`;
  return query(sql);
};

// 文章列表
const getBookslist = async (params) => {
  const data = {
    pageCode: params.pageCode ? params.pageCode : 1,
    pageSize: params.pageSize ? params.pageSize : 10,
    search: params.search ? params.search : "",
  };
  let sql = await query(
    `select * from bookslist a join doctorlist b on a.textdoctor = b.doctorid where texttitle like '%${
      data.search
    }%' limit ${(data.pageCode - 1) * data.pageSize}, ${data.pageSize}`
  );
  let sql1 = await query(`select * from bookslist`);
  return { data: sql, total: sql1.length };
};
// 文章阅读量
const bookBrowse = (params) => {
  let sql = `UPDATE bookslist SET textcount = textcount + 1 WHERE textid = '${params.id}'`;
  return query(sql);
};
// 文章详情
const getBookDetail = (params) => {
  let sql = `select * from bookslist a join doctorlist b join soco c on a.textdoctor = b.doctorid and b.doctoradmin = c.socoid where a.textid = '${params.id}'`;
  return query(sql);
};
// 文章查询
const getBookSearch = (params) => {
  let sql = `select * from bookslist where textdoctor = '${params.id}'`;
  return query(sql);
};
// 文章删除
const bookDelete = (params) => {
  let sql = `delete from bookslist where textid = ${params.id}`;
  return query(sql);
};
// 文章新增
const bookAdd = (params) => {
  console.log(params);
  let sql = `INSERT INTO bookslist (texttitle,textdescword,textdatetime,textimage,textcount,texttype,textdise,textaudio,textdoctor) VALUES ('${params.title}','${params.word}','${params.time}','${params.image}','${params.count}','${params.type}','${params.dise}','${params.audio}','${params.doctor}')`;
  return query(sql);
};
// 文章修改
const bookUpdate = (params) => {
  let sql = `update bookslist set texttitle = '${params.title}',textdescword = '${params.word}',textdatetime = '${params.time}',textimage = '${params.image}',textcount = '${params.count}',texttype = '${params.type}',textdise = '${params.dise}',textaudio = '${params.audio}',textdoctor = '${params.doctor}' where textid = '${params.id}'`;
  return query(sql);
};

// 医生列表
const getDoctorlist = async (params) => {
  const data = {
    pageCode: params.pageCode ? params.pageCode : 1,
    pageSize: params.pageSize ? params.pageSize : 10,
    city: params.city ? params.city : "",
    admin: params.admin === "全部" ? "" : params.admin ? params.admin : "",
    tag:
      params.tag === "1"
        ? "a.doctorgood"
        : params.tag === "2"
        ? "a.doctorbrowse"
        : "a.doctortime",
    order: params.order ? params.order : "asc",
    name: params.name ? params.name : "",
  };
  let sql = await query(
    `select * from doctorlist a join soco b on a.doctoradmin = b.socoid where a.doctorname like '%${
      data.name
    }%' and a.doctorcity like '%${data.city}%' and a.doctoradministre like '%${
      data.admin
    }%' order by ${data.tag} ${data.order} limit ${
      (data.pageCode - 1) * data.pageSize
    }, ${data.pageSize}`
  );
  let sql1 = await query(`select * from doctorlist`);
  return { data: sql, total: sql1.length };
};
// 医生详情
const getDoctorDetail = (params) => {
  let sql = `select * from doctorlist a join bookslist b join soco c on a.doctoradmin = c.socoid and a.doctorid = b.textdoctor where a.doctorid = '${params.id}'`;
  return query(sql);
};
// 关注医生
const attention = (params) => {
  let sql = `update doctorlist set doctorflag = '${params.doctorflag}' where doctorid = '${params.id}'`;
  query(sql);
  let sql1 = `select * from doctorlist where doctorid = '${params.id}'`;
  let sql2 = "";
  query(sql1).then((resp) => {
    let res = resp[0];
    if (params.doctorflag === "1") {
      sql2 = `INSERT INTO doctorlist_like (doctoriddoc,doctorname,doctordescword,doctoradministre,doctorcity,doctorimage,doctorstate,doctoradmin,doctorprice,doctorgood,doctorbrowse,doctortime,doctorflag) VALUES ('${res.doctoriddoc}','${res.doctorname}','${res.doctordescword}','${res.doctoradministre}','${res.doctorcity}','${res.doctorimage}','${res.doctorstate}','${res.doctoradmin}','${res.doctorprice}','${res.doctorgood}','${res.doctorbrowse}','${res.doctortime}',${res.doctorflag})`;
      query(sql2);
    } else {
      if (res.doctoriddoc) {
        query(
          `select * from doctorlist_like where doctoriddoc = '${res.doctoriddoc}'`
        ).then((res) => {
          if (res.length) {
            sql2 = `DELETE FROM doctorlist_like WHERE doctoriddoc = '${res[0].doctoriddoc}'`;
            query(sql2);
          }
        });
      }
    }
  });
  return params.doctorflag;
};
// 获取已关注的医生
const getDoctorlistLike = (params) => {
  let sql = `SELECT * FROM doctorlist_like`;
  return query(sql);
};

// 抛出
module.exports = {
  nosocomiumList,
  getBookslist,
  getDoctorlist,
  nosocomiumDetail,
  getBookDetail,
  getDoctorDetail,
  attention,
  getDoctorlistLike,
  nosocoDelete,
  nosocoAdd,
  nosocoUpdate,
  bookDelete,
  bookBrowse,
  getBookSearch,
  bookAdd,
  bookUpdate,
};
